<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class categories_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    public function getCategories(){
        // Run the query
        $query = $this->db->query('SELECT * FROM categories WHERE deleted = \'0\'');
        
		//log_message('info', '$query->num_rows = ' . $query->num_rows);		
        
		
		return $query->result();
    }

    function deleteCategory($id) {
        log_message('info','Trying to delete category: ' . $id);
        
        if(isset($id)) {
            $sql = 'UPDATE categories SET deleted = \'1\' WHERE id = ?';
            $this->db->query($sql,$id);
            
            return $this->db->affected_rows();    
        } else {
            return null;
        }        
    }
	
	function saveCategory($data) {
		$catname = $data['catname'];
		$catdesc = $data['catdesc'];
		$userid = $data['userid'];
		$catid = $data['catid'];
		
		if($catid == '0') {
			$sql = 'INSERT INTO categories (category,description) VALUES (?,?)';
        	$this->db->query($sql, array($catname, $catdesc));
		} else {
			$sql = 'UPDATE categories SET category=?,description=? WHERE id=?';
        	$this->db->query($sql, array($catname, $catdesc,$catid));
		}
		
		
		
		return $this->db->affected_rows();
	}

}
?>